package com.aichongbang.acb.service.impl;

import com.aichongbang.acb.po.Permission;
import com.aichongbang.acb.po.Role;
import com.aichongbang.acb.po.User;
import com.aichongbang.acb.mapper.UserMapper;
import com.aichongbang.acb.service.IUserService;
import com.aichongbang.acb.vo.UserVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author acb
 * @since 2021-05-27
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserVO login(String username, String password) {
        User user = userMapper.login(username, password);
        UserVO userVO = new UserVO();
        userVO.setTId(user.getTId());
        userVO.setTUsername(user.getTUsername());
        userVO.setTPassword(user.getTPassword());
        userVO.setTName(user.getTName());
        userVO.setTTel(user.getTTel());
        userVO.setTEmail(user.getTEmail());

        List<Role> roleList = new ArrayList<>();
        for (int i = 0; i < user.getUserRoleList().size(); i++) {
            Role role = user.getUserRoleList().get(i).getRole();

            roleList.add(role);
            userVO.setRoleList(roleList);
        }
        List<Permission> permissionList = new ArrayList<>();
        for (int i = 0; i < user.getRolePermissionList().size(); i++) {
            Permission permission = user.getRolePermissionList().get(i).getPermission();

            permissionList.add(permission);
            userVO.setPermissionList(permissionList);
        }
        return userVO;
    }

    @Override
    public List<UserVO> getAllUserPermission() {
        List<User> userList = userMapper.getAllUserPermission();
        List<UserVO> userVOList = new ArrayList<>();
        UserVO userVO = new UserVO();
        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            userVO.setTId(user.getTId());
            userVO.setTUsername(user.getTUsername());
            userVO.setTTel(user.getTTel());
            userVO.setTEmail(user.getTEmail());
            userVO.setTName(user.getTName());

            List<Role> roleList = new ArrayList<>();
            for (int j = 0; j < user.getUserRoleList().size(); j++) {
                Role role = user.getUserRoleList().get(j).getRole();
                roleList.add(role);
            }
            userVO.setRoleList(roleList);

            List<Permission> permissionList = new ArrayList<>();
            for (int k = 0; k < user.getRolePermissionList().size(); k++) {
                Permission permission = user.getRolePermissionList().get(k).getPermission();
                permissionList.add(permission);
            }
            userVO.setPermissionList(permissionList);
            userVOList.add(userVO);

        }
        return userVOList;
    }

    @Override
    public Integer addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public User getUserByUsername(String username) {
        User user = userMapper.getUserByUsername(username);
        return user;
    }

    @Override
    public List<UserVO> getUserLike(User user) {
        List<User> userList = userMapper.getUserLike(user);
        List<UserVO> userVOList = new ArrayList<>();
        UserVO userVO = new UserVO();
        for (int i = 0; i < userList.size(); i++) {
            User user1 = userList.get(i);
            userVO.setTId(user1.getTId());
            userVO.setTUsername(user1.getTUsername());
            userVO.setTTel(user1.getTTel());
            userVO.setTEmail(user1.getTEmail());
            userVO.setTName(user1.getTName());

            Role role;
            List<Role> roleList = new ArrayList<>();
            for (int j = 0; j < user1.getUserRoleList().size(); j++) {
                role = user1.getUserRoleList().get(j).getRole();
                roleList.add(role);
            }
            userVO.setRoleList(roleList);

            List<Permission> permissionList = new ArrayList<>();
            for (int k = 0; k < user1.getRolePermissionList().size(); k++) {
                Permission permission = user1.getRolePermissionList().get(k).getPermission();
                permissionList.add(permission);
            }
            userVO.setPermissionList(permissionList);

            userVOList.add(userVO);

        }
        return userVOList;
    }

    @Override
    public boolean updateUser(User user) {
        int n = userMapper.updateById(user);
        return n > 0 ? true : false;
    }

    @Override
    public boolean deletedUser(Integer tId) {
        int n = userMapper.deleteById(tId);
        return n > 0 ? true : false;
    }


}
